<template>
  <!-- 我负责详情页 -->
  <div class="box">
    <div class="details_top clearfix">
      <span
        class="left"
        v-if="name.length < 3"
      >
        {{
          name
            .split("")
            .reverse()
            .join("")
            .substring(0, 1)
            .split("")
            .reverse()
            .join("")
        }}</span>
      <span
        class="left"
        v-else-if="name.length >= 3"
      >
        {{
          name
            .split("")
            .reverse()
            .join("")
            .substring(0, 2)
            .split("")
            .reverse()
            .join("")
        }}</span>
      <ul class="left">
        <li>{{ list.create_name }}</li>
        <li>{{ list.create_time }}</li>
        <li>{{ list.task_title }}</li>
      </ul>
    </div>
    <p class="details_content">{{ list.task_desc }}</p>
    <!-- 列表 -->
    <div class="details_list">
      <ul>
        <li>
          <span>任务类型</span><span v-if="list.is_recruit == 1">指派任务</span>
          <span v-else-if="list.is_recruit==0">招募任务</span>
          <span v-else-if="list.is_recruit==2">多人任务</span>
        </li>
        <li style="position:relative;">
          <div style="width:120px;position:relative;left:0px;top:0x">
            发布范围
          </div>
          <div style="width:200px;position:absolute;left:118px;top:0px">
            {{ list.scope }}
          </div>
        </li>
        <li v-if="list.is_recruit == 0">
          <span>领取得分</span><span>{{ D_name }}:{{ list.receive_code_d }}</span><span
            style="margin-left:10px"
            v-if="list.receive_code_c != 0 && ISC == 1"
          >{{ C_name }}:{{ list.receive_code_c }}</span>
        </li>
      </ul>
    </div>
    <!-- 图片 -->
    <van-image-preview
      v-model="show"
      :images="images"
      @change="onChange"
    >
      <template v-slot:index>{{ index + 1 }}/{{ images.length }}</template>
    </van-image-preview>
    <div
      class="details_list"
      style="border-bottom:1px solid #c0c0c04d"
      v-if="images.length != 0"
    >
      <span style="display: inline-block;width: 120px;">图片</span><span
        v-for="(img, s) in images"
        :key="s"
        style="display:inlin-block"
      ><img
          @click="show_file(img)"
          style="height:80px;margin-right:5px;vertical-align:middle"
          :src="img"
        /></span>
    </div>
    <!-- 附件 -->
    <div
      class="details_list"
      style="border-bottom:1px solid #c0c0c04d"
      v-if="file.length != 0"
    >
      <span style="display: inline-block;width: 120px;">附件</span><span
        v-for="(m, l) in file"
        :key="l"
        style="display:inline-block"
      >
        <span style="padding:8px;line-height:80px;height:80px;display:inline-block;border:1px solid #c0c0c04d">
          <div
            @click="openLink(m)"
            style="color:blue"
          >附件下载</div>
          <!-- <a :href="m">
            附件下载
          </a> -->
        </span>
      </span>
    </div>
    <!-- 子任务 -->
    <div class="details_list">
      <ul>
        <li style="position:relative;">
          <div style="width:120px;position:relative;left:0px;top:0x">
            负责人
          </div>
          <div style="width:200px;position:absolute;left:120px;top:0px">
            {{ list.user_name }}
            <img
              @click="show_test(list)"
              style="width:13px;margin-left:5px"
              src="@/static/section/zan_selected@2x.png"
            />
          </div>
        </li>
        <li>
          <span>开始时间</span><span>{{ list.start_time }}</span>
        </li>
        <li>
          <span>结束时间</span><span>{{ list.end_time }}</span>
        </li>
        <li v-if="u_status == 1">
          <span>完成得分</span>
          <span
            v-if="change_text_d!=0"
            style="margin-left:-5px"
          >
            {{D_name}}:<input
              type="number"
              v-model="text_change_D"
              placeholder="0"
              style="width:50px;border:1px solid silver;border-radius:10%;text-align:center"
            />(此处改分)
          </span>
        </li>
         <li v-if="u_status == 1&&change_text_c!=0">
          <div style="margin-left:120px"> 
            {{C_name}}:<input
              type="number"
              v-model="text_change_C"
              placeholder="0"
              style="width:50px;border:1px solid silver;border-radius:10%;text-align:center"
            />(此处改分)</div>
        </li>
        <li>
          <span>逾期扣分</span><span>{{ D_name }}:{{ list.overtime_code_d }}/天</span><span
            style="margin-left:10px"
            v-if="list.overtime_code_c != 0 && ISC == 1"
          >{{ C_name }}:{{ list.overtime_code_c }}/天</span>
        </li>
        <li>
          <span>实际得分</span><span>{{ D_name }}:{{ list.u_complete_code_d }}</span><span
            style="margin-left:10px"
            v-if="list.u_complete_code_c != 0 && ISC == 1"
          >{{ C_name }}:{{ list.u_complete_code_c }}</span>
        </li>
        <li>
          <span>逾期上限</span><span>{{ list.overtime_end_day }}天</span>
        </li>
        <li>
          <span>验收人</span><span>{{ list.check_name }}</span>
        </li>
      </ul>
    </div>
    <div
      class="details_list"
      v-if="u_status == 1"
    >
      <span style="display:inline-block;width:120px;">提交时间</span><span>{{ list.submit_time }}</span>
    </div>
    <div
      class="details_list"
      v-if="u_status == 1"
      @click="show_end"
    >
      <span style="display:inline-block;width:120px;">审核人</span><span v-if="arr.length == 0">审核人</span><span>{{ arr.name }}</span>
      <van-icon name="arrow" />
    </div>
    <van-field
      v-if="u_status == 1"
      v-model="message"
      rows="3"
      autosize
      label="验收意见"
      type="textarea"
      placeholder="请输入验收意见"
      show-word-limit
    />
    <div v-if="u_status == 1">
      <van-button
        plain
        size="large"
        type="info"
        @click="show_disagree"
      >驳回</van-button>
      <van-button
        size="large"
        type="info"
        @click="show_submission"
      >同意</van-button>
    </div>
  </div>
</template>

<script type="text/javascript">
import {
  Icon,
  Form,
  Field,
  Uploader,
  Button,
  Popup,
  RadioGroup,
  Radio,
  Search,
  Toast
} from "vant";
import "dingtalk-jsapi/entry/mobile";
import openLink from "dingtalk-jsapi/api/biz/util/openLink";
export default {
  data() {
    return {
      text_change_D: 0,
      text_change_C: 0,
      change_text_d: 0,
     change_text_c: 0,
      list: [],
      message: "",
      value_people_two: "",
      endlist: [],
      radio: 0,
      arr: [],
      C_name: "C分",
      D_name: "D分",
      change_text: 0,
      text_id: 0,
      u_status: 0,
      images: [],
      file: [],
      show: false,
      index: 0,
      name: "",
      ISC: ""
    };
  },
  created() {
    this.getUserId();
    dd.biz.navigation.setTitle({
      title: "详情" //控制标题文本，空字符串表示显示默认文本
    });
  },
  beforeUpdate() {
    this.C_name = localStorage.getItem("c_name");
    this.D_name = localStorage.getItem("d_name");
  },
  methods: {
    // 下载附件
    openLink(m) {
      dd.biz.util.openLink({
        url: m
      });
    },
    //C分开关
    async getIsC() {
      let corpId = localStorage.getItem("custid");
      const res = await this.$http.get(`yuepoints/getIsC?corpId=${corpId}`);
      // localStorage.setItem("ISC", res.data.data.is_c_code);
      this.ISC = res.data.data.is_c_code;
    },
    // 钉钉鉴权
    async show_jsapi() {
      let corpId = localStorage.getItem("custid");
      // let corpId = this.$utils.getUrlKey("custid");
      let url = location.href.split("#")[0];
      const res = await this.$http.get(
        `dingapi/getJsapiConfig?corpId=${corpId}&url=${url}`
      );
      let config = res.data.data;
      console.log(config);
      localStorage.setItem("appId", config.agentId);
      dd.config({
        agentId: config.agentId,
        corpId: config.corpId,
        timeStamp: config.timeStamp,
        nonceStr: config.nonceStr,
        signature: config.signature,
        jsApiList: [
          "runtime.info",
          "biz.ding.post",
          "biz.ding.create",
          "biz.chat.pickConversation",
          "device.notification.prompt",
          "device.notification.confirm",
          "device.notification.alert",
          "biz.navigation.setRight",
          "biz.chat.open",
          "biz.util.open",
          "biz.util.openLink",
          "biz.user.get",
          "biz.contact.choose",
          "biz.telephone.call",
          "biz.util.uploadImage",
          // "device.accelerometer.watchShake",
          // "biz.map.locate",
          // "device.geolocation.get",
          "biz.customContact.choose",
          "biz.contact.complexPicker",
          "biz.contact.departmentsPicker"
        ]
      });
    },
    // 自定义名称存储
    async getCustomName() {
      let corpId = localStorage.getItem("custid");
      const res = await this.$http.get(
        `yuepoints/getCustomName?corpId=${corpId}`
      );
      if (res.data.errcode == "0000") {
        if (res.data.data.c_name == "") {
          localStorage.setItem("c_name", "C分");
        } else {
          localStorage.setItem("c_name", res.data.data.c_name);
        }
        if (res.data.data.d_name == "") {
          localStorage.setItem("d_name", "D分");
        } else {
          localStorage.setItem("d_name", res.data.data.d_name);
        }
      } else {
        localStorage.setItem("c_name", "C分");
        localStorage.setItem("d_name", "D分");
      }
    },
    //鉴权
    async getUserId() {
      let Corp_Id = localStorage.getItem("custid");
      let User_Id = localStorage.getItem("userId");
      let corpId = this.$utils.getUrlKey("custid");
      if (corpId == null || corpId == "undefined") {
        corpId = Corp_Id;
      } else {
        localStorage.setItem("custid", corpId);
      }
      var _this = this;
      let code = "";
      if (User_Id && User_Id != "undefined" && User_Id != null) {
        if (Corp_Id != corpId) {
          await dd.runtime.permission.requestAuthCode({
            corpId: corpId,
            onSuccess: function(result) {
              code = result.code;
            },
            onFail: function(err) {}
          });
          _this.$http
            .get(`dingapi/signIn?corpId=${corpId}&code=${code}`)
            .then(res => {
              localStorage.setItem("userId", res.data.data.dd_id);
              localStorage.setItem("custid", corpId);
              _this.getCustomName();
              _this.taskHallInfo();
              _this.show_jsapi();
              _this.getIsC();
            });
        } else {
          _this.getCustomName();
          _this.taskHallInfo();
          _this.show_jsapi();
          _this.getIsC();
        }
      } else {
        await dd.runtime.permission.requestAuthCode({
          corpId: corpId,
          onSuccess: function(result) {
            code = result.code;
          },
          onFail: function(err) {}
        });
        _this.$http
          .get(`dingapi/signIn?corpId=${corpId}&code=${code}`)
          .then(res => {
            localStorage.setItem("userId", res.data.data.dd_id);
            localStorage.setItem("custid", corpId);
            _this.getCustomName();
            _this.taskHallInfo();
            _this.show_jsapi();
            _this.getIsC();
          });
      }
    },
    show_file(img) {
      this.show = true;
    },
    onChange(index) {
      this.index = index;
    },
    // 驳回
    async show_disagree() {
      let userId = localStorage.getItem("userId");
      let corpId = localStorage.getItem("custid");
      let codeD = 0;
      let codeC = 0;
      if (this.change_text_d != 0) {
        codeD = this.text_change_D;
      }
      // if (this.change_text_d == text_change_D) {
      //   codeD = "";
      // }
      if (this.change_text_c != 0) {
        codeC = this.text_change_C;
      }
      // if (this.change_text_c == this.text_change) {
      //   codeC = "";
      // }
      Toast.loading({
        message: "数据提交中",
        forbidClick: true,
        loadingType: "spinner"
      });
      const res = await this.$http.get(
        `yuepoints/taskAcceptance?corpId=${corpId}&userId=${userId}&id=${this.text_id}&desc=${this.message}&codeD=${codeD}&codeC=${codeC}&status=2&approval=${this.arr.emplId}`
      );
      if (res.data.errcode == "0000") {
        Toast.success("审核成功");
        this.$router.push({
          //核心语句
          path: "/task",
          query: {
            //路由传参时push和query搭配使用 ，作用时传递参数
            active: 3
          }
        });
      }
    },
    // 同意验收
    async show_submission() {
      let userId = localStorage.getItem("userId");
      let corpId = localStorage.getItem("custid");
      let codeD = 0;
      let codeC = 0;
      if (this.change_text_d != 0) {
        codeD = this.text_change_D;
      }
      // if (this.change_text_d == this.text_change) {
      //   codeD = "";
      // }
      if (this.change_text_c != 0) {
        codeC = this.text_change_C;
      }
      // if (this.change_text_c == this.text_change) {
      //   codeC = "";
      // }
      Toast.loading({
        message: "数据提交中",
        forbidClick: true,
        loadingType: "spinner"
      });
      const res = await this.$http.get(
        `yuepoints/taskAcceptance?corpId=${corpId}&userId=${userId}&id=${this.text_id}&desc=${this.message}&codeD=${codeD}&codeC=${codeC}&status=3&approval=${this.arr.emplId}`
      );
      if (res.data.errcode == "0000") {
        Toast.success("验收成功");
        this.$router.push({
          //核心语句
          path: "/task",
          query: {
            //路由传参时push和query搭配使用 ，作用时传递参数
            active: 3
          }
        });
      } else {
        Toast(res.data.errmsg);
      }
    },
    // 跳转点赞
    show_test(it) {
      this.$router.push({
        //核心语句
        path: "/test",
        query: {
          //路由传参时push和query搭配使用 ，作用时传递参数
          dd_id: it.user_id,
          dd_name: it.user_name
        }
      });
    },
    //审核人
    async show_end() {
      let corpId = localStorage.getItem("custid");
      let appId = localStorage.getItem("appId");
      let userId = localStorage.getItem("userId");
      let _this = this;
      const res = await _this.$http.get(
        `yuepoints/getReviewer?corpId=${corpId}&userId=${userId}&type=2`
      );
      _this.endlist = res.data.data.end;
      let users = [];
      for (var i = 0; i < _this.endlist.length; i++) {
        users.push(_this.endlist[i].dd_id);
      }
      dd.biz.customContact.choose({
        title: "审核人", //标题
        users: users, //一组员工userid
        corpId: corpId, //加密的企业 ID，
        isShowCompanyName: true, //true|false，默认为 false
        // disabledUsers: ["78308"], //不能选的人
        onSuccess: function(data) {
          // _this.start_name =data[0].name;
          _this.arr = data[0];
        },
        onFail: function(err) {}
      });
    },
    // 默认展示数据
    async taskHallInfo() {
      let userId = localStorage.getItem("userId");
      let corpId = localStorage.getItem("custid");
      if (userId == null || userId == "undefined") {
        // Toast("请在钉钉工作台悦积分入口进入查看");
        this.$router.replace("/error");
        return false;
      }
      const id = this.$route.query.id;
      const res = await this.$http.get(
        `yuepoints/acceptanceTaskInfo?corpId=${corpId}&userId=${userId}&id=${id}`
      );
      const { data } = res.data;
      this.list = data;
      this.name = this.list.create_name;
      if (data.complete_code_d != 0) {
        this.text_change_D = data.complete_code_d;
      }
      if (data.complete_code_c != 0) {
        this.text_change_C = data.complete_code_c;
      }
      // console.log(this.text_change_D, this.text_change_C);
      this.change_text_d = data.complete_code_d;
      this.change_text_c = data.complete_code_c;
      this.text_id = data.uid;
      this.u_status = data.ustatus;
      if (!this.isEmpty(data.img_url)) {
        if (data.img_url.indexOf(",") > -1) {
          this.images = data.img_url
            .split(",")
            .reverse()
            
        } else {
          this.images.push(data.img_url);
        }
      }
      if (!this.isEmpty(data.file_url)) {
        this.file.push(data.file_url);
        if (data.file_url.indexOf(",") > -1) {
          this.file = data.file_url
            .split(",")
            .reverse()
            .splice(1)
            .reverse();
        }
      }
    },
    isEmpty(obj) {
      if (typeof obj == "undefined" || obj == null || obj == "") {
        return true;
      } else {
        return false;
      }
    }
  },
  components: {
    [Icon.name]: Icon,
    [Form.name]: Form,
    [Field.name]: Field,
    [Uploader.name]: Uploader,
    [Button.name]: Button,
    [Popup.name]: Popup,
    [RadioGroup.name]: RadioGroup,
    [Radio.name]: Radio,
    [Search.name]: Search
  }
};
</script>

<style lang="less" scoped>
.box {
  font-size: 15px;
  padding-bottom: 15px;

  /deep/ .van-button--large {
    width: 48%;
    height: 40px;
    line-height: 40px;
  }
  /deep/ .van-field__value {
    margin-left: 42px;
  }
  /deep/ .van-icon-arrow {
    float: right;
  }
  /deep/ .van-cell__title{
    text-align: left;
    padding-left: 15px;
    font-size: 16px;
  }
  /deep/ .van-field__value {
    margin-left: 42px;
    font-size: 16px;
  }
  .personnel_text {
    display: inline-block;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: #59d1ff;
    font-size: 14px;
    line-height: 42px;
    color: #fff;
  }
  /deep/ .van-radio {
    background: #fff;
    // border-bottom: 1px solid #e6e3e3;
    padding: 5px 10px;
  }
  .personnel_title {
    position: relative;
    bottom: 0px;
    left: 10px;
  }
  .details_top {
    text-align: left;
    padding: 30px 30px 10px;
    background: #fff;
    span {
      display: inline-block;
      width: 50px;
      height: 50px;
      border-radius: 50%;
      text-align: center;
      line-height: 50px;
      background-color: #59d1ff;
      color: #fff;
    }
    ul {
      margin-left: 10px;
      background: #fff;
    }
    li:nth-child(2) {
      font-size: 12px;
      color: silver;
    }
    li:nth-child(3) {
      font-size: 16px;
      margin-top: 5px;
    }
  }
  .details_content {
    margin: 0px;
    text-align: left;
    padding: 10px 30px;
    font-size: 14px;
    background: #fff;
  }
  .details_list {
    font-size: 16px;
    text-align: left;
    padding: 15px 20px 15px 30px;
    border-top: 1px solid silver;
    // border-bottom: 1px solid silver;
    background: #fff;
    li {
      margin-bottom: 5px;
      span:nth-child(1) {
        display: inline-block;
        width: 120px;
      }
    }
  }
  .details_comment {
    margin: 10px 30px;
    .details_comment_title {
      span {
        font-size: 13px;
        display: inline-block;
        width: 40px;
        height: 40px;
        border-radius: 50%;
        text-align: center;
        line-height: 40px;
        background-color: #59d1ff;
        color: #fff;
      }
      ul {
        margin-left: 10px;
      }
      li:nth-child(2) {
        font-size: 12px;
        color: silver;
      }
    }
    p {
      text-align: left;
    }
  }
}
.left {
  float: left;
}
.right {
  float: right;
}
.clearfix::after {
  content: "";
  display: block;
  height: 0;
  clear: both;
  visibility: hidden;
}
.clearfix {
  zoom: 1;
}
</style>
